package Hangman;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import javax.ejb.EJB;
import javax.naming.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.omg.CosNaming.NamingContextExtHelper;

/**
 *
 * @author stuartsealfon
 */
@WebServlet(name="WelcomeServlet", urlPatterns={"/WelcomeServlet"})
public class WelcomeServlet extends HttpServlet {
    @EJB
    //private NamingContext context;
    private HangmanBeanRemote hangmanBean;
    private UserBeanRemote userBean;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            if (request.getParameter("name") != null && request.getParameter("password") != null) {
                userBean = new UserBean();
                userBean.setInfo(request.getParameter("name"), request.getParameter("password"));

                if (request.getParameter("register") != null) {
                    //out.println(request.getParameter("password2"));
                    hangmanBean.register(request.getParameter("name"), request.getParameter("password"), request.getParameter("password2"));
                }
            }
            //out.println(request.getSession());


            out.println("<html>");
            out.println("<head>");
            out.println("<title>Welcome!</title>");
            //out.println("<meta http-equiv=\"refresh\" content=\"10\">");

            /*out.println("<style type=\"text/css\"> body {margin:0;}");
            out.println("#wrap {width:300px;height:120px;background-color:#CCCCCC;");
            out.println("position:absolute;top:50%;left:50%;margin-top:-60px;");
            out.println("margin-left:-150px;} </style>");*/
            out.println("</head>");

            out.println("<body onload=\"setTimeout('document.refreshPage.submit()',10000);\">");
            out.println("<h1><center>Last Man Standing</center></h1>");
            out.println("<center>");

            java.util.Date date = new java.util.Date();
            out.println("Today's date and time: " + date + "<br><br>");

            if (request.getParameter("name") != null && !request.getParameter("name").equals("null")) out.println("Hi, " + request.getParameter("name") + "<br>");

            if (hangmanBean.login(request.getParameter("name"), request.getParameter("password")) == true) {
                out.println(hangmanBean.seeUsersInside() + "<br><br><br>");
                if (request.getParameter("remove") != null) {
                    if (!request.getParameter("word").equals("Admin")) hangmanBean.remove(request.getParameter("word"));
                }

                if (request.getParameter("name").equals("Admin")) {
                    out.println("<form method=\"post\" action=\"WelcomeServlet\">"
                    + "<input type=\"hidden\" name=\"name\" value=" + request.getParameter("name")
                    + "><input type=\"hidden\" name=\"password\" value=" + request.getParameter("password")
                    + ">Choose a User to remove: <input type=\"text\" name=\"word\">"
                    + "<input type=\"submit\" name=\"remove\" value=\"Remove a User\">"
                    + "</form><br><br><br>");
                }
                else {
                    out.println("<form method=\"post\" action=\"gameBoard2\">"
                    + "<input type=\"hidden\" name=\"name\" value=" + request.getParameter("name")
                    + "><input type=\"hidden\" name=\"password\" value=" + request.getParameter("password")
                    + ">Enter Word: <input type=\"text\" name=\"word\">"
                    + "<input type=\"submit\" name=\"submit\" value=\"Start New Game/Join a Game\">"
                    + "</form><br><br><br>");
                }

                out.println("<form method=\"post\" action=\"index.jsp\">"
                    + "<input type=\"hidden\" name=\"name\" value=" + request.getParameter("name")
                    + "><input type=\"hidden\" name=\"password\" value=" + request.getParameter("password")
                    + "><input type=\"submit\" name=\"logout\" value=\"Log Out\">"
                    + "</form><br><br><br>");
            }
            else {
                out.println("<br><br><br><font color=\"red\">Invalid Username or Password!</font>");
                out.println("<form action =\"index.jsp\" method=\"post\">");
                out.println("<input type=\"submit\" name=\"submit\" value=\"Return to Login Screen\">");
                out.println("</form>");
            }

            out.println("<form name=\"refreshPage\" action=\"WelcomeServlet\""
                    + "ID=\"refreshPage\" method=\"post\" name=\"test\"> "
                    + "<input type=\"hidden\" name=\"name\" value=" + request.getParameter("name")
                    + "><input type=\"hidden\" name=\"password\" value=" + request.getParameter("password")
                    + "></form>");

            out.println("</body>");
            out.println("</html>");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
